package com.baseframe.sys.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baseframe.common.utils.PageUtils;
import com.baseframe.common.utils.R;
import com.baseframe.common.validator.ValidatorUtils;
import com.baseframe.common.validator.group.AddGroup;
import com.baseframe.common.validator.group.UpdateGroup;
import com.baseframe.sys.entity.AppClass;
import com.baseframe.sys.entity.AppInfo;
import com.baseframe.sys.entity.AppUserRelation;
import com.baseframe.sys.entity.FrameRole;
import com.baseframe.sys.service.IAppClassService;
import com.baseframe.sys.service.IAppInfoService;
import com.baseframe.sys.service.IAppUserRelationService;
import com.baseframe.sys.service.dao.FrameRoleRepository;

/**
 * 注册应用
 * @author mzl
 */
@RestController
@RequestMapping("/sys/appinfo")
public class AppInfoController extends AbstractController {
	@Autowired
	private IAppInfoService service;
	@Autowired
	private IAppClassService classService;
	@Autowired
    private FrameRoleRepository frameRoleRepository;
	@Autowired
	private IAppUserRelationService appRelationService;
	
	/**
	 * 所有用户列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = service.queryPage(params);
		return R.ok().put("page", page);
	}
	
	/**
	 * 详情
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") int id){
		AppInfo appinfo = service.selectById(id);
		//查询类别名称
		AppClass appclass=classService.selectById(appinfo.getAppclass());
		appinfo.setAppclassName(appclass.getClassname());
		return R.ok().put("appinfo", appinfo);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody AppInfo appinfo){
		ValidatorUtils.validateEntity(appinfo, AddGroup.class);
		service.save(appinfo);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody AppInfo appinfo){
		ValidatorUtils.validateEntity(appinfo, UpdateGroup.class);

		service.update(appinfo);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Integer[] ids){
		for(int guid : ids){
			service.delete(guid);
		}
		return R.ok();
	}
	
	@RequestMapping("/appRoleList/{appid}")
    public R moduleRoleList(@PathVariable("appid") int appid) {
        List<FrameRole> frameRoleList = frameRoleRepository.findAll();
        List<Integer> optional = appRelationService.getAllowByType(appid, "Role");
        Map<String, Object> roleMap = new HashMap<>(1);
        roleMap.put("roleList", frameRoleList);
        roleMap.put("roleIds", optional);
        return R.ok().put("roleMap", roleMap);
    }
	
	@RequestMapping("saveAppRight")
    @Transactional
    public R saveAppRight(@RequestBody JSONObject appRight) {
        List<String> roleIds = appRight.getJSONArray("roleIds").toJavaList(String.class);
        Integer appid = appRight.getInteger("appid");

        appRelationService.deleteByAppidAndAllowtype(appid, "Role");

        roleIds.forEach(roleGuid -> {
        	AppUserRelation relation = new AppUserRelation();
        	relation.setAllowto(roleGuid);
        	relation.setAppid(appid);
            relation.setAllowtype("Role");
            appRelationService.save(relation);
        });
        return R.ok();
    }
}
